//
//  CustomBlueTextView.m
//  wtshoppingcite
//
//  Created by Baojia-LC on 14/11/4.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "CustomBlueTextView.h"

@implementation CustomBlueTextView

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (id)initWithFrame:(CGRect)frame withPlaceHolder:(NSString*)placeholder
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self.layer setCornerRadius:5];
        self.clipsToBounds = YES;
        self.layer.borderColor = MAIN_ORANGE_COLOR.CGColor;
        self.layer.borderWidth = 1.5;
        self.delegate = self;
        
        _placeHolderLabel =  [[UILabel alloc] initWithFrame:CGRectMake(5, 0, self.width, 30)];
        _placeHolderLabel.backgroundColor = [UIColor clearColor];
        _placeHolderLabel.font = [UIFont systemFontOfSize:14];
        _placeHolderLabel.textColor = COLOR_RGB(164, 164, 164);
        _placeHolderLabel.text = placeholder;
        [self addSubview:_placeHolderLabel];
    }
    return self;
}

-(void)setTextViewBorderColor:(UIColor*)color{
    
    self.layer.borderColor = color.CGColor;
    self.layer.borderWidth = 1.5;
    
}

- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    if (![text isEqualToString:@""])
    {
        _placeHolderLabel.hidden = YES;
    }
    if ([text isEqualToString:@""] && range.location == 0 && range.length == 1) {
        _placeHolderLabel.hidden = NO;
    }
    
    if ([text isEqualToString:@"\n"]) {
        if(range.location == 0 && range.length == 0){
            _placeHolderLabel.hidden = NO;
        }
        [textView resignFirstResponder];
        return NO;
    }
    return YES;
}

@end
